/*=============================================================================*
 *  Copyright 2004 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *=============================================================================*/
package topics.impl;

import topics.Topic;
import topics.TopicSpace;
import topics.TopicSpaceSet;
import topics.expression.TopicExpression;
import topics.expression.TopicExpressionEngine;
import topics.expression.TopicExpressionException;
import topics.expression.impl.TopicExpressionEngineImpl;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;

/**
 * @author Sal Campana
 */
public class TopicSpaceSetImpl
   implements TopicSpaceSet
{
   private static final TopicExpressionEngine TOPIC_EXPR_ENGINE = TopicExpressionEngineImpl.getInstance(  );
   private Map                                m_topicSpaces = new Hashtable(  );
   private boolean                            m_fixed;

   /**
    * Creates a new {@link TopicSpaceSetImpl} object.
    *
    * @param fixed DOCUMENT_ME
    */
   public TopicSpaceSetImpl( boolean fixed )
   {
      m_fixed = fixed;
   }

   /**
    * DOCUMENT_ME
    *
    * @param fixed DOCUMENT_ME
    */
   public void setFixed( boolean fixed )
   {
      m_fixed = fixed;
   }

   /**
    * DOCUMENT_ME
    *
    * @return DOCUMENT_ME
    */
   public boolean isFixed(  )
   {
      return m_fixed;
   }

   /**
    * DOCUMENT_ME
    *
    * @param namespaceURI DOCUMENT_ME
    *
    * @return DOCUMENT_ME
    */
   public TopicSpace getTopicSpace( String namespaceURI )
   {
      return (TopicSpace) m_topicSpaces.get( namespaceURI );
   }

   /**
    * DOCUMENT_ME
    *
    * @return DOCUMENT_ME
    */
   public TopicSpace[] getTopicSpaces(  )
   {
      return (TopicSpace[]) m_topicSpaces.values(  ).toArray( new TopicSpace[0] );
   }

   /**
    * Adds the TopicSpace to the TopicSpaceSet.
    *
    * Note. If the TopiSpace already exists, this method will add all the known Topics
    * to the passed-in TopicSpace and set it as the current TopicSpace in the TopicSpaceSet.
    * This will ensure there is only 1 TopicSpace in the set and it is complete...
    *
    * @param topicSpace
    */
   public TopicSpace addTopicSpace( TopicSpace topicSpace )
   {
      String namespaceURI = topicSpace.getTargetNamespace(  );

      //if topicSpace exists, simply add all topics to topicspace
      if ( m_topicSpaces.containsKey( namespaceURI ) )
      {
         TopicSpace topicSpc = (TopicSpace) m_topicSpaces.get( namespaceURI );
         Iterator   iterator = topicSpc.topicIterator(  );
         while ( iterator.hasNext(  ) )
         {
            Topic topic = (Topic) iterator.next(  );
            topicSpace.addTopic( topic );
         }
      }

      m_topicSpaces.put( namespaceURI, topicSpace );
      return topicSpace;
   }

   /**
    * DOCUMENT_ME
    *
    * @param topicExpr DOCUMENT_ME
    *
    * @return DOCUMENT_ME
    *
    * @throws TopicExpressionException DOCUMENT_ME
    */
   public Topic[] evaluateTopicExpression( TopicExpression topicExpr )
   throws TopicExpressionException
   {
      return TOPIC_EXPR_ENGINE.evaluateTopicExpression( this, topicExpr );
   }

   /**
    * DOCUMENT_ME
    *
    * @param namespaceURI DOCUMENT_ME
    */
   public void removeTopicSpace( String namespaceURI )
   {
      m_topicSpaces.remove( namespaceURI );
   }
}